package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.vo.SkuInfoVo;
import com.atguigu.gmall.starter.cache.service.CacheService;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.builder.ResultMapResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author tangsy
 * @Description TODO
 * @date 2022/12/2 16:52
 * @Version 1.0
 */
@Api(tags = "sku")
@RestController
@RequestMapping("admin/product")
public class SkuInfoController {

    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    CacheService cacheService;

    @GetMapping("/updateSkuInfo")
    public Result updateSku(@RequestBody SkuInfoVo vo){

        //1修改数据库

        //2删除缓存
        cacheService.delayDoubleDel("sku:info:49");

        return Result.ok();
    }

    @ApiOperation("分页获取sku")
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuPage(@PathVariable("page") Long page,
                             @PathVariable("limit") Long limit){
        Page<SkuInfo> skuInfoPage = skuInfoService.page(new Page<SkuInfo>(page, limit));
        return Result.ok(skuInfoPage);
    }

    ///admin/product/saveSkuInfo
    @ApiOperation("添加sku")
    @PostMapping("saveSkuInfo")
    public Result save(@RequestBody SkuInfoVo skuInfoVo){
        skuInfoService.saveSkuInfo(skuInfoVo);
        return Result.ok();
    }

    //admin/product/onSale/{skuId}
    @ApiOperation("上架")
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId){
        skuInfoService.onSale(skuId);
        return Result.ok();
    }

    //admin/product/cancelSale/{skuId}
    @ApiOperation("下架")
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId){
        skuInfoService.cancelSale(skuId);
        return Result.ok();
    }

}
